/**
 * Created on [2022-12-08] by zhengrr
 *
 * 项目名称: btfvilg
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 *
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * ©2021 福建慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunnada.btfvilg.entity.Word;
import com.sunnada.btfvilg.mapper.WordMapper;
import com.sunnada.btfvilg.model.response.WordVo;
import com.sunnada.btfvilg.service.WordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 *  服务实现类
 *
 * @author zhengrr
 */
@Service
public class WordServiceImpl extends ServiceImpl<WordMapper, Word> implements WordService {

//    private Queue<WordVo> words = null;

    private List<Word> words = null;

    @Autowired(required = false)
    private WordMapper wordMapper;

    private String basePath = "D:\\work\\fzu\\1-课程\\1-英语\\单词\\";
    private Integer site = 0;
    private Integer mode = 0; //0: all  1: 课后

    @Override
    public Word nextWord() {
//        if(words!=null && site==words.size()){
//            reset();
//        }
        if(site==words.size()){
            return new Word("请重置","请充值",0,0);
        }
        if(words==null){
            getWords(0);
        }
        Word word = words.get(site++);
        if(mode == 0) wordMapper.updateTurn(word.getWord(), word.getTurn()+1);
        return word;
    }

   void getWords(int mode){
        QueryWrapper<Word> qw = new QueryWrapper<>();
        if(mode == 0){  //全部
            this.mode = 0;
            qw.orderByAsc("turn");
            words = wordMapper.selectList(qw);
            site = 0;
            Random random = new Random();
            for (int i = 0; i < words.size(); i++) {
                words.get(i).setRandom(random.nextInt(20));
            }
            Collections.sort(words, new Comparator<Word>() {
                @Override
                public int compare(Word o1, Word o2) {
                    int temp = o1.getTurn() - o2.getTurn();
                    if(temp == 0) return o1.getRandom() - o2.getRandom();
                    else return temp;
                }
            });
        }else{
            this.mode = 1;
            qw.eq("type",1).orderByAsc("turn");
            words = wordMapper.selectList(qw);
            site = 0;
            Random random = new Random();
            for (int i = 0; i < words.size(); i++) {
                words.get(i).setRandom(random.nextInt(20));
            }
            Collections.sort(words, new Comparator<Word>() {
                @Override
                public int compare(Word o1, Word o2) {
                    return o1.getRandom() - o2.getRandom();
                }
            });
        }
    }

    @Override
    public WordVo reset() {
        words = null;
        site = 0;
        return null;
    }

    @Override
    public Integer loadNewWord() {
        int sum = 0;
        Integer minTurn = wordMapper.getMinTurn();
        if(minTurn == null) minTurn = 0;
        try {
            String line;
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(basePath+"loadNewWord\\newWords.txt"),"utf-8"
                    )
            );
            while((line=br.readLine()) != null){
                String[] split = line.split("=");
                if(split.length != 2) return 99999;
                try {
                    wordMapper.insert(new Word(split[0],split[1],minTurn,0));
                    sum++;
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return sum;
    }

    @Override
    public void changeMode(String mode) {
        words = null;
        site = 0;
        if("全部".equals(mode)){
            getWords(0);
        }else{
            getWords(1);
        }
    }

}
